/* Source
 * Datei: tools.c
 * Info: Hilfsroutinen
 * Autor: 
 * Datum: 
 *
 */

#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include "tools.h"

uint8_t toBCD(uint8_t in)
{
    return ((in / 10)<<4) | (in % 10);
}

uint8_t fromBCD(uint8_t in)
{
    uint8_t t;
    t  = (in & 0xF0) >> 1;
    t += (t >> 2);
    return ((in & 0x0F) + t);
}



// Hilfsfunktion zur Anzeige von Hex-zahlen
void to_hex(uint8_t in, char *out)
{
        out[0] = (in >> 4 ) + '0' ;
        if (out[0] > '9')
                out[0] += 'A'-'0'-10; //

        out[1] = (in & 0x0F) + '0' ;
        if (out[1] > '9')
                out[1] += 'A'-'0'-10; //
        out[2] = 0x00;
}

void to_hex_word(uint16_t in, char *out)
{
    to_hex((char)(in>>8), out);
    to_hex((char)in, &out[2]);
}

